<template>
  <div class="app-container">
    <el-row :gutter="10" class="el-row-inline">
      <!--部门数据-->
      <el-col :span="5" :xs="24">
        <el-card style="height:100%">
          <div class="head-container">
            <el-input
              v-model="subjectName"
              placeholder="请输入学院/专业名称"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
            />
          </div>
          <div class="head-container">
            <el-tree
              :data="subjectOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="tree"
              node-key="id"
              default-expand-all
              highlight-current
              @node-click="handleNodeClick"
            />
          </div>
        </el-card>
      </el-col>
      <el-col :span="19" :xs="24">
        <el-card class="search-card">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="课程名称" prop="courseName">
              <el-input
                v-model="queryParams.courseName"
                placeholder="请输入课程名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="启用状态" prop="isEnable">
              <el-select v-model="queryParams.isEnable" placeholder="请选择是否启用：0-未启用  1-启用" clearable>
                <el-option
                  v-for="dict in dict.type.sys_open_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="课程老师" prop="isEnable">
              <el-select v-model="queryParams.courseTeacherId" placeholder="请选择课程老师" clearable>
                <el-option
                  v-for="item in listTechers"
                  :key="item.userId"
                  :label="item.nickName"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>

        <el-card class="table-card">
          <el-row>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="subjectCourseList" @selection-change="handleSelectionChange">
            <el-table-column label="序号" align="center" prop="id" />
            <el-table-column label="学院" align="center" prop="depName" />
            <el-table-column label="专业" align="center" prop="subjectName" />
            <el-table-column label="课程代码" align="center" prop="courseCode" />
            <el-table-column label="课程名称" align="center" prop="courseName" />
            <el-table-column label="课程介绍" align="center" prop="content" />
<!--            <el-table-column label="课程教师" align="center" prop="courseTeacher" />-->
            <el-table-column label="启用状态" align="center" prop="isEnable">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_open_status" :value="scope.row.isEnable"/>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNo"
            :limit.sync="queryParams.pageLimit"
            @pagination="getList"
          />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listSubjectCourse } from "@/api/information/subjectCourse";
import { getSubjectTree } from '@/api/information/subject'

export default {
  name: "SubjectCourse",
  dicts: ['sys_open_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专业课程管理表格数据
      subjectCourseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageLimit: 10,
        depId: null,
        subjectId: null,
        courseName: null,
        courseTeacher: null,
        isEnable: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      subjectOptions: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      subjectName: '',
    };
  },
  watch: {
    // 根据名称筛选部门树
    subjectName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.findTree();
  },
  methods: {
    findTree(){
      getSubjectTree().then(res => {
        this.subjectOptions = res.data;
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      // 可以点击父子级，所以需要打印出来看看结构再写逻辑
      if (data.children == null) {
        this.queryParams.deptId = null;
        this.queryParams.subjectId = data.id;
      } else {
        this.queryParams.deptId = data.id;
        this.queryParams.subjectId = null;
      }
      this.handleQuery();
    },

    /** 查询专业课程管理列表 */
    getList() {
      this.loading = true;
      listSubjectCourse(this.queryParams).then(response => {
        this.subjectCourseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        depId: null,
        subjectId: null,
        courseName: null,
        content: null,
        courseTeacher: null,
        isEnable: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</script>
